package com.platform.controller;

import com.platform.entity.GoodsEntity;
import com.platform.entity.OrderEntity;
import com.platform.entity.OrderGoodsEntity;
import com.platform.service.GoodsService;
import com.platform.service.OrderGoodsService;
import com.platform.service.OrderService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-13 10:41:09
 */
@RestController
@RequestMapping("order")
public class OrderController extends AbstractController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private GoodsService goodsService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("order:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);

        //中大团队扩展
        if(!isSuperAdmin()) {
            query.put("rootDeptId", getRootDept().getDeptId());
        }

        List<OrderEntity> orderList = orderService.queryList(query);
        int total = orderService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("order:info")
    public R info(@PathVariable("id") Integer id) {
        OrderEntity order = orderService.queryObject(id);
        //中大团队扩展
        Integer orderId = order.getId();
        Map perms = new HashMap();
        perms.put("order_id", orderId);
        List<OrderGoodsEntity> orderGoodsEntityList = orderGoodsService.queryList(perms);
        OrderGoodsEntity orderGoods = orderGoodsEntityList.get(0);

        Map result = new HashMap();
        result.put("order", order);
        result.put("orderGoods", orderGoods);


        return R.ok(result);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.update(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:delete")
    public R delete(@RequestBody Integer[] ids) {
        orderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<OrderEntity> list = orderService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = orderService.queryTotal(params);

        return R.ok().put("sum", sum);
    }

    /**
     * 确定收货
     *
     * @param id
     * @return
     */
    @RequestMapping("/confirm")
    @RequiresPermissions("order:confirm")
    public R confirm(@RequestBody Integer id) {
        orderService.confirm(id);

        return R.ok();
    }

    /**
     * 发货
     *
     * @param order
     * @return
     */
    @RequestMapping("/sendGoods")
    @RequiresPermissions("order:sendGoods")
    public R sendGoods(@RequestBody OrderEntity order) {
        orderService.sendGoods(order);

        //中大团队扩展
        Map perms = new HashMap();
        perms.put("order_id", order.getId());
        List<OrderGoodsEntity> orderGoodsEntityList = orderGoodsService.queryList(perms);
        OrderGoodsEntity orderGoodsEntity = orderGoodsEntityList.get(0);
        Integer goodsId = orderGoodsEntity.getGoodsId();
        Integer orderNum = orderGoodsEntity.getNumber();
        GoodsEntity goodsEntity = goodsService.queryObject(goodsId);
        Integer currNum = goodsEntity.getCollageCurrNum();
        goodsEntity.setCollageCurrNum(currNum - orderNum);
        goodsService.save(goodsEntity);

        return R.ok();
    }

    /**
     * 确认退款
     *
     * @param id
     * @return
     */
    @RequestMapping("/refund")
    @RequiresPermissions("order:refund")
    //中大团队扩展
    public R refund(@RequestBody Integer id) {
        OrderEntity order = orderService.queryObject(id);
        order.setOrderStatus(404);
        orderService.update(order);
        return R.ok();
    }
}
